package spl.manager;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.List;

import spl.manager.JobsContainer.Job;
import spl.utils.GlobalLogger;
import spl.utils.MimeType;

public class Representation {
	private ArrayList<Job> jobsTrack;
	private Job jobCreator;
	private int resourceId;
	private int representationId;
	
	private File representationFile;
	
	/**
	 * C'tor - called for the first representation
	 * @param representaionResourceId is the resource id for this representation 
	 * @param repFile is the file object of this representation
	 */
	public Representation(int representaionResourceId, File repFile) {
		init();
		
		this.resourceId = representaionResourceId;
		this.representationFile = repFile;
		this.representationId = 0;
	}
	
	/**
	 * C'tor
	 * @param resId resource id
	 * @param repId representation id
	 */
	public Representation(int resId, int repId) {
		init();
		
		this.representationId = repId;
		this.resourceId = resId;
	}
	
	/**
	 * When representation is uploaded, sets it's file that
	 * we wish to server.
	 * @param repFile file path of new representation
	 */
	public void setRepresentationFile(File repFile) {
		this.representationFile = repFile;
	}

	private void init() {
		this.jobCreator = null;
		this.jobsTrack = new ArrayList<Job>();
	}

	/**
	 * @return the representation's id
	 */
	public int getId() {
		return this.representationId;
	}
	
	/**
	 * @return this resource id
	 */
	public int getResId() {
		return this.resourceId;
	}

	/**
	 * @return the jobs inside the container
	 */
	public List<Job> getJobs() {
		return this.jobsTrack;
	}
	
	/**
	 * @return the job that creates this representation
	 */
	public Job getCreatingJob() {
		return this.jobCreator;
	}
	
	/**
	 * @param creatingJob keep track after the job
	 */
	public void setCreatingJob(Job creatingJob) {
		this.jobCreator = creatingJob;
	}
	
	/**
	 * @return a stream of the file content
	 */
	public FileInputStream getContent() {
		FileInputStream f;
		
		// File doesn't yet exist
		if (null == this.representationFile)
			return null;
		
		try {
			f = new FileInputStream(this.representationFile);
		} catch (FileNotFoundException e) {
			GlobalLogger.getLogger().warning("Couldn't find file " + this.representationFile.getName());
			return null;
		}
		
		return f;
	}
	
	/**
	 * @return file size
	 */
	public int getSize() {
		// File doesn't yet exist
		if (null == this.representationFile)
			return 0;
		
		
		return (int)this.representationFile.length(); 
	}

	/**
	 * @return Mime type associated with this file
	 */
	public String getMime() {
		// File doesn't yet exist
		if (null == this.representationFile)
			return "text/html";
		
		
		return MimeType.DB.getType(this.representationFile.getName());
	}
	
	/**
	 * Keeps track after this representaion's jobs 
	 * @param j is a valid job object for this representation
	 */
	public void recordJob(Job j) {
		this.jobsTrack.add(j);
	}

}
